<?php
namespace Admin\Controller;
use Think\Controller;
header('content-type:text/html;charset="UTF-8"');
class CustondeliveryController extends CommonController {
    public function index(){
    	$db=M('order');
    	
        $data=$db->where("status='已付款' and custondelivery='否' and delivery='已收货' or delivery='已发货'")->select();
       
        $timeArray = array( );
        foreach ($data as $key => $value) {

            if (!in_array($value['time'], $timeArray)) {
               array_push($timeArray,$value['time'] );
            }
        }

    	foreach ($timeArray as $key => $time) {
            $where = array(
                        'time' => $time, 
                        'status' => '已付款',
                        );
            $data=$db->where($where)->select();
            foreach ($data as $k => $val) {
               $val['property']=json_decode($val['property'],true);
               $data[$k]['property']=$val['property'];
            }
            $orderArray[$key]['time']=$time;
            $orderArray[$key]['data']=$data;
            //$orderArray[$key]['status']=$data[0]['status']; 
            //$orderArray[$key]['delivery']=$data[0]['delivery'];
            $orderArray[$key]['images']=$data[0]['images'];
            $orderArray[$key]['delivery']=$data[0]['delivery'];             
        }
        rsort($orderArray);
        foreach ($orderArray as $key => $value) {
            if(empty($value['data'])){
                unset($orderArray[$key]);
            }
        }
        $this->assign('orderArray',$orderArray);
       
        $this->display();

        
    }
    public function changeDelivery(){
    	$time=I('get.time');	
    	$db=M('order');
    	$where = array(
    		'time'=>$time,
    		'status' => '已付款', 
    		'delivery'=>'已收货',
        'custondelivery'=>'否'
    		);
        $data=$db->where($where)->select();

       	foreach ($data as $key => $value) {
       		$custondelivery['custondelivery']="是";
       		$ret=$db->where("id={$value['id']}")->save($custondelivery);
       	}
       	if ($ret==true) {
       		$this->success('确认客户收货成功','',2);
       	}


    }
}